#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <boost/date_time/posix_time/posix_time.hpp>

#include "logger.hpp"
#include "channel.hpp"
#include "etcd.hpp"
#include "utils.hpp"

#include "base.pb.h"
#include "user.pb.h"
#include "file.pb.h"
#include "message.pb.h"

DEFINE_bool(run_mode, true, "是否为调试模式");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(message_service, "/service/message_service", "服务信道目录");

class MessageClientTest: public ::testing::Test {
protected:
    void SetUp() override {
        auto sm = std::make_shared<im::ServiceManager>();
        sm->declared(FLAGS_message_service);
        auto put_cb = std::bind(&im::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
        auto del_cb = std::bind(&im::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
        im::Discover::ptr discover = std::make_shared<im::Discover>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
        // 初始化服务发现
        _channel = sm->choose(FLAGS_message_service);
        if (!_channel) {
            im::LOG_CRITICAL("未发现关注的服务，测试退出");
            abort() ;
        }
    }

    void TearDown() override {
    }

    im::ServiceChannel::ChannelPtr _channel;
};

TEST_F(MessageClientTest, GetHistoryMsg) {
    im::MsgStorageService_Stub stub(_channel.get());

    im::GetHistoryMsgReq req;
    std::string rid = im::uuid();
    std::string csid = "会话ID1";
    req.set_request_id(rid);
    req.set_chat_session_id(csid);
    time_t stime = boost::posix_time::to_time_t(boost::posix_time::time_from_string("2025-03-12 00:00:00"));
    time_t etime = boost::posix_time::to_time_t(boost::posix_time::time_from_string("2025-03-17 00:00:00"));
    req.set_start_time(stime);
    req.set_over_time(etime);
    
    im::GetHistoryMsgRsp rsp;

    brpc::Controller cntl;
    stub.GetHistoryMsg(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    std::cout << "-----------------------获取区间消息--------------------------\n";
    for (int i = 0; i < rsp.msg_list_size(); i++) {
        printf("------------------------%d------------------------\n", i+1);
        auto msg = rsp.msg_list(i);
        std::cout << msg.message_id() << std::endl;
        std::cout << msg.chat_session_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(boost::posix_time::from_time_t(msg.timestamp())) << std::endl;
        std::cout << msg.sender().user_id() << std::endl;
        std::cout << msg.sender().nickname() << std::endl;
        std::cout << msg.sender().avatar() << std::endl;
        if (msg.message().message_type() == im::MessageType::STRING) {
            std::cout << "文本消息：" << msg.message().string_message().content() << std::endl;
        }else if (msg.message().message_type() == im::MessageType::IMAGE) {
            std::cout << "图片消息：" << msg.message().image_message().image_content() << std::endl;
        }else if (msg.message().message_type() == im::MessageType::FILE) {
            std::cout << "文件消息：" << msg.message().file_message().file_contents() << std::endl;
            std::cout << "文件名称：" << msg.message().file_message().file_name() << std::endl;
        }else if (msg.message().message_type() == im::MessageType::SPEECH) {
            std::cout << "语音消息：" << msg.message().speech_message().file_contents() << std::endl;
        }else {
            std::cout << "类型错误！！\n";
        }
    }
}

TEST_F(MessageClientTest, GetRecentMsg) {
    im::MsgStorageService_Stub stub(_channel.get());

    im::GetRecentMsgReq req;
    std::string rid = im::uuid();
    std::string csid = "会话ID1";
    req.set_request_id(rid);
    req.set_chat_session_id(csid);
    req.set_msg_count(2);
    
    im::GetRecentMsgRsp rsp;

    brpc::Controller cntl;
    stub.GetRecentMsg(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    std::cout << "-----------------------获取 最近2条 消息--------------------------\n";
    for (int i = 0; i < rsp.msg_list_size(); i++) {
        printf("------------------------%d------------------------\n", i+1);
        auto msg = rsp.msg_list(i);
        std::cout << msg.message_id() << std::endl;
        std::cout << msg.chat_session_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(boost::posix_time::from_time_t(msg.timestamp())) << std::endl;
        std::cout << msg.sender().user_id() << std::endl;
        std::cout << msg.sender().nickname() << std::endl;
        std::cout << msg.sender().avatar() << std::endl;
        if (msg.message().message_type() == im::MessageType::STRING) {
            std::cout << "文本消息：" << msg.message().string_message().content() << std::endl;
        }else if (msg.message().message_type() == im::MessageType::IMAGE) {
            std::cout << "图片消息：" << msg.message().image_message().image_content() << std::endl;
        }else if (msg.message().message_type() == im::MessageType::FILE) {
            std::cout << "文件消息：" << msg.message().file_message().file_contents() << std::endl;
            std::cout << "文件名称：" << msg.message().file_message().file_name() << std::endl;
        }else if (msg.message().message_type() == im::MessageType::SPEECH) {
            std::cout << "语音消息：" << msg.message().speech_message().file_contents() << std::endl;
        }else {
            std::cout << "类型错误！！\n";
        }
    }
}

TEST_F(MessageClientTest, MsgSearch) {
    im::MsgStorageService_Stub stub(_channel.get());

    im::MsgSearchReq req;
    std::string rid = im::uuid();
    std::string csid = "会话ID1";
    req.set_request_id(rid);
    req.set_chat_session_id(csid);
    req.set_search_key("关联");
    
    im::MsgSearchRsp rsp;

    brpc::Controller cntl;
    stub.MsgSearch(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    std::cout << "-----------------------关键字搜索消息--------------------------\n";
    for (int i = 0; i < rsp.msg_list_size(); i++) {
        printf("------------------------%d------------------------\n", i+1);
        auto msg = rsp.msg_list(i);
        std::cout << msg.message_id() << std::endl;
        std::cout << msg.chat_session_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(boost::posix_time::from_time_t(msg.timestamp())) << std::endl;
        std::cout << msg.sender().user_id() << std::endl;
        std::cout << msg.sender().nickname() << std::endl;
        std::cout << msg.sender().avatar() << std::endl;
        std::cout << "文本消息：" << msg.message().string_message().content() << std::endl;
    }
}

int main(int argc, char *argv[]) {
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    im::initLogger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
    
    return RUN_ALL_TESTS();
}